package com.springbootbase.springbootproject.service.impl;

import com.springbootbase.springbootproject.mapper.TeacherMapper;
import com.springbootbase.springbootproject.pojo.ListResponse;
import com.springbootbase.springbootproject.pojo.Teacher;
import com.springbootbase.springbootproject.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;


@Service
public class TeacherImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public Teacher getInfoById(Long id) {
        Teacher teacher = teacherMapper.findById(id);
        return teacher;
    }

    @Override
    public void updateTeacher(Teacher teacher) {
        teacherMapper.updateTeacher(teacher);
    }

    @Override
    public ListResponse getList(Long id, int pageSize, int pageNum) {
        int offset = (pageNum-1)*pageSize;
        ArrayList<Teacher> teacherArrayList = teacherMapper.getList(id,offset,pageSize);
        for (Teacher i : teacherArrayList){
            String name = teacherMapper.getNameById(i.getId());
            i.setName(name);
        }
        int lsSize = teacherMapper.countAllTeacherInfo(id);
        ListResponse listResponse = new ListResponse(teacherArrayList,lsSize);
        return listResponse;
    }


}
